package zt.song.shopping.filter;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @Author 宋伟宁
 * @Date 2023/12/7
 * @Version 1.0
 **/
public class LoginFilter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session
        HttpSession session = request.getSession();
        //获取请求的uri
        String requestURI = request.getRequestURI();
        System.out.println("请求的uri："+requestURI);
        if(requestURI.endsWith("login") ||
                requestURI.endsWith("register") ||
                requestURI.endsWith("list") ||
                requestURI.endsWith("jpg") ||
                requestURI.endsWith("css") ||
                requestURI.endsWith("js") ||
                session.getAttribute("user")!=null){
             return true;
        }else{
            //重定向到登录（注册）
            response.sendRedirect("/login");
        }

        return  false;
    }
}
